/*
 * @Description:
 * @Author: youzi
 * @Date: 2020-06-18 14:16:25
 * @LastEditors: youzi
 * @LastEditTime: 2020-07-20 10:29:52
 * @todo:
 */
class FileOperation {
  constructor() {}
  /**
   * @param {object} that 组件实例that
   * @desc: 渲染文件列表
   * @author: youzi
   */
  renderFileList(that) {
    // debugger;
    if (!Object.keys(that.initRecord).length) {
      return;
    }
    that.formItem.forEach(el => {
      if (el.type === "file") {
        let key = el.decorator + "List";
        if (!!that.initRecord[key].length) {
          that.initRecord[key].forEach(file => {
            let { id, fileName, filePath, fileType } = file;
            let tmpObj = {};
            let url = that.filePreURL + filePath + "." + fileType;
            tmpObj.uid = -id;
            tmpObj.id = id;
            tmpObj.name = fileName + "." + fileType;
            tmpObj.url = url;
            tmpObj.fileType = fileType;
            tmpObj.status = "done";
            el.fileList.push(tmpObj);
          });
          /* let tmpObj = {};
          tmpObj[el.dataIndex] = el.fileList;
          that.form.setFieldsValue(tmpObj); */
        }
      }
    });
  }
  /**
   * @param {object} rule 校验规则
   * @param {object} val 当前文件组件内部的值，可能为空
   * @param {function} callback 必须调用的回调函数
   * @param {string} title 文件标签
   * @param {boolean} required 是否必填
   * @desc: 文件校验
   * @author: youzi
   */
  handleFileValidate(rule, val, callback, title, required) {
    if (!required) {
      return;
    }
    if (val && val.fileList && val.fileList.length === 0) {
      return title + "不能为空";
    }
    return;
  }
  /**
   * @desc: 文件状态改变时触发的函数
   * @param {object} that 组件实例
   * @param {string} name 文件属性名
   * @param {number} index 文件在渲染列表中的下标
   * @param {object} info file对象，包含当前文件组件的一切
   * @author: youzi
   */
  handleChange(that, name, index, info) {
    // console.log(name, info);
    let fileList = info.fileList;
    let file = info.file;
    if (file.status === "removed") {
      this.removeFileInFE(that, name, index, file);
      return;
    } else {
      if (file.size > 100 * 1024 * 1024) {
        // 文件不大于100MB
        that.$message.error("文件过大，上传失败");
        return false;
      }
      that.loading = true;
      const formData = new FormData();
      formData.append("files", file);
      that
        .$upload(that.apiURL.uploadFile, formData)
        .then(r => {
          let data = r.data.data;
          if (r.data.code === 200) {
            let { id, fileName, filePath, fileType } = data[0];
            let url = that.filePreURL + filePath + "." + fileType;
            file.id = id;
            file.url = url;
            file.fileType = fileType;
            that.formItem[index].fileList.push(file);
            that.loading = false;
            that.$message.success("上传文件成功");
          }
        })
        .catch(r => {
          console.error(r);
          this.removeFileInFE(that, name, index, file);
          that.loading = false;
        });
    }
  }
  /**
   * @description: 阻止ant-design自带的自动上传事件
   * @return: false
   * @editor: youzi
   * @Date: 2019-09-17 11:32:38
   */
  beforeUpload(file, fileList) {
    // that.loading = true;
    // that.renderData[index].fileList = fileList;
    return false;
  }
  /**
   * @desc: 预览文件函数
   * @param {object} file 文件实例
   * @param {object} that 组件实例
   * @author: youzi
   */
  handlePreview(that, file) {
    // console.warn(file);
    that.$download(that.apiURL.downloadFile, { id: file.id }, file.name);
  }
  /**
   * 用于前端删除文件的函数
   * @param {object} that 组件实例
   * @param {string} name 文件所属的属性名
   * @param {number} index 属性名在渲染数组中所处的位置
   * @param {object} file 文件
   * @author: youzi
   * @Date: 2019-10-25 14:13:18
   */
  removeFileInFE(that, name, index, file) {
    // console.log(that.form.getFieldsValue());
    // const fileIndex = that.renderData[index].fileList.indexOf(file);
    let nowFileList = that.formItem[index].fileList;
    let fileIndex = -1;
    for (let i = 0; i < nowFileList.length; i++) {
      const el = nowFileList[i];
      if (el.uid === file.uid) {
        fileIndex = i;
        break;
      }
    }
    if (fileIndex === -1) {
      return;
    }
    const newFileList = that.formItem[index].fileList.slice();
    newFileList.splice(fileIndex, 1);
    that.formItem[index].fileList = newFileList;
    let key = that.formItem[index].decorator;
    that.form.setFieldsValue({ [key]: newFileList });
    // that.removeFile(file);
    that.$message.success("删除文件成功");
  }
  handleRemove(name, index, file) {
    if (file.status === "error") {
      // that.apiURL.deleteFile(file);
      console.log("error");
      this.removeFileInFE(name, index, file);
      return;
    }
    // console.log('delete');
    that
      .$post(that.apiURL.deleteFile, { fileName: file.filenameEn })
      .then(r => {
        this.removeFileInFE(name, index, file);
      })
      .catch(r => {
        if (r.data.code === 10001) {
          this.removeFileInFE(name, index, file);
        }
        console.error(r);
        // that.removeFile(file);
        // that.uploading = false;
      });
  }
}

export default FileOperation;
